<?php
/**
 * doitphp tools 登录
 */
class LoginController extends Controller {		
	
	/**
	 * 登陆界面
	 */
	public function indexAction() {
		//parse login
		$this->parse_login();
		
		//set lay out
		$this->set_layout();
		
		//display page
		$this->display();
	}
	
	/**
	 * 处理登陆
	 */
	public function handle_loginAction() {
		
		//session start
		session_start();
		
		//get params
		$user_name 	= $this->post('user_name');
		$password	= $this->post('user_password');
		$vdcode		= strtolower($this->post('vd_code'));
		
		//分析检验码
		if (strtolower(session::get('doit_tools_vdcode')) != $vdcode) {
			echo 105;
			exit();
		}
		
		//load login config file		
		if ($user_name == DOITPHP_ADMIN_USER && $password == DOITPHP_ADMIN_PASSWORD) {
			cookie::set('doit_tools_login_state', true, 7200);
			echo 101;
		} else {
			echo 100;
		}		
	}
	
	/**
	 * 验证码页面
	 */
	public function vdcodeAction() {
		
		//load image_lib clss
		$image_lib = $this->instance('pincode');
		
		$image_lib->set_session_name('doit_tools_vdcode')->show();
	}
	
	/**
	 * 注销（登出）
	 */
	public function logoutAction() {
		
		cookie::set('doit_tools_login_state', false);
		
		//跳转至登陆页面
		$this->redirect($this->get_action_url('index'));
	}
	
	/**
	 * 分析是否登陆
	 */
	public function parse_login() {
		
		$login_state = cookie::get('doit_tools_login_state');
		
		if ($login_state == true) {
			//跳转向首页
			$this->redirect($this->create_url('index/index'));
		}
		
		return true;
	}
}